package cn.caixiong.org.service.impl;


import cn.caixiong.basic.config.exception.BusinessException;
import cn.caixiong.basic.service.Impl.BaseServiceImpl;
import cn.caixiong.org.domain.Employee;
import cn.caixiong.org.domain.Shop;
import cn.caixiong.org.mapper.EmployeeMapper;
import cn.caixiong.org.mapper.ShopMapper;
import cn.caixiong.org.query.ShopQuery;
import cn.caixiong.org.service.IShopService;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.digest.MD5;
import com.sun.xml.internal.ws.util.xml.ContentHandlerToXMLStreamWriter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.prefs.BackingStoreException;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop,ShopQuery> implements IShopService  {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeemapper;
    @Override
    public void settlement(Shop shop) {
        //1.新增店长的信息到t_shop表,可以拿到店长的id
        Employee admin = shop.getAdmin();
        if (!StringUtils.equals(admin.getPassword(),admin.getComfirmPassword())){
             throw new BusinessException("两次输入密码不一致");
        }
        //获取手机号
        boolean mobile = Validator.isMobile(admin.getPhone());
        //获取邮箱
        boolean email = Validator.isEmail(admin.getEmail());
        if (!mobile){
            throw new BusinessException("手机号有问题");
        }
        if (!email){
            throw new BusinessException("邮箱有问题");

        }
        //验证店铺重复
      List<Employee> employee = shopMapper.selectOrusername(admin);
      if (CollectionUtil.isNotEmpty(employee)){
          throw new BusinessException("你有重复信息,请检查一下");
      }

        admin.init();
        employeemapper.insertSelective(admin);
        //2.新增店铺信息(可以拿到主键)店长的id通过第一步拿到
        Long adminId = admin.getId();
        shop.setAdminId(adminId);
        //3.修改员工,为了设置shop_id外检的值
        shopMapper.insertSelective(shop);
        admin.setShopId(shop.getId());
        employeemapper.updateByPrimaryKeySelective(admin);
    }

}
